﻿#include <iostream>
#include <vector>
using namespace std;


class Solution {
public:
	// 43. 字符串相乘
    string multiply(string num1, string num2)
    {
        if (num1 == "0" || num2 == "0")
        {
            return "0";
        }
        int len1 = num1.size(), len2 = num2.size();
        // 创造一个存储结果的容器
        auto result = vector<int>(len1 + len2);
        // 通过全排列两个字符串
        for (int i = len1 - 1; i >= 0; --i)
        {
            int x = num1[i] - '0';
            for (int j = len2 - 1; j >= 0; --j)
            {
                int y = num2[j] - '0';
                // 相乘得出位数上的数值
                result[i + j + 1] += x * y;
            }
        }
        // 控制位数上的值在0~9区间，多出的数值进位
        for (int i = len1 + len2 - 1; i > 0; --i)
        {
            result[i - 1] += result[i] / 10;
            result[i] %= 10;
        }
        // 最高位为零从1开始遍历，避免拷贝数值0
        int index = result[0] == 0 ? 1 : 0;
        string ret;
        while (index < len1 + len2)
        {
            ret.push_back(result[index]);
            ++index;
        }
        // 数值转化为字符
        for (auto& ch : ret)
        {
            ch += '0';
        }
        return ret;
    }


	// 118. 杨辉三角
	vector<vector<int>> generate(int numRows)
	{
		vector<vector<int>> v;
		// 构建numRows行，行内数据类型是vector<int>
		v.resize(numRows, vector<int>());
		for (size_t i = 0; i < v.size(); ++i)
		{
			// 根据行数开辟每行列数并初始化
			v[i].resize(i + 1, 0);
			v[i][0] = v[i][v[i].size() - 1] = 1;
		}
		// 根据行列数据判断是否需要计算
		for (size_t i = 0; i < v.size(); ++i)
		{
			for (size_t j = 0; j < v[i].size(); ++j)
			{
				if (v[i][j] == 0)
				{
					v[i][j] = v[i - 1][j] + v[i - 1][j - 1];
				}
			}
		}
		return v;
	}


	// 136. 只出现一次的数字
	int singleNumber(vector<int>& nums) 
	{
		int ret = 0;
		for (auto i : nums)
		{
			ret ^= i;
		}
		return ret;
	}
};


void TestVector1()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	for (size_t i = 0; i < v1.size(); ++i)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	vector<int>::iterator it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int> copy = v1;
	for (auto e : copy)
	{
		cout << e << " ";
	}
	cout << endl;
}

void TestVector2()
{
	vector<int> v1(10, 1);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int> v2(v1.begin(), v1.end() - 5);
	for (auto e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	string s1("hello world");
	vector<char> v3(s1.begin(), s1.end());
	for (auto e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
}

void TestVector3()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	vector<int>::iterator it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//vector<int>::reverse_iterator rit = v1.rbegin();
	auto rit = v1.rbegin();
	while (rit != v1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}

int main()
{
	//TestVector1();
	//TestVector2();
	TestVector3();

	return 0;
}

